<template>
  <div class="description-container">
    <div class="header-container">
      <van-icon name="arrow-left" class="back-icon" @click="goBack"/>
      <h2>问题描述</h2>
    </div>
    <div class="feedback-container">
      <textarea v-model="pojo.feedbackContent" placeholder="请输入您要反馈的问题"></textarea>
    </div>
    <button @click="submitFeedback">提交反馈</button>
  </div>
</template>

<script>
import axios from "axios";
import { Toast } from "vant";

export default {
  data() {
    return {
      pojo: {
        feedbackContent: ''
      },
      user: {}
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    submitFeedback() {
      // 非空校验
      if (!this.pojo.feedbackContent.trim()) {
        Toast.fail('请填写反馈内容');
        return;
      }

      // 确保用户数据已加载
      if (!this.user || !this.user.id) {
        Toast.fail('用户信息加载失败，请稍后再试');
        return;
      }

      this.pojo.id = this.user.id;
      // 在这里处理提交逻辑
      axios.post(`http://localhost:10086/api/admin/feedback`, this.pojo)
          .then(response => {
            console.log(response.data);
            // 提交成功
            Toast.success('反馈提交成功');
            this.$router.go(-1);
          })
          .catch(error => {
            console.error("There was an error!", error);
            Toast.fail('反馈提交失败');
          });
    }
  },
  created() {
    // 取值 userDB
    const userDB = sessionStorage.getItem("userDB");
    if (userDB) {
      try {
        this.user = JSON.parse(userDB);
      } catch (error) {
        console.error("解析用户数据失败:", error);
      }
    }
  }
};
</script>

<style scoped>
.description-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 20px;
}

.back-icon {
  margin-right: 10px;
}

h2 {
  margin: 0;
  flex: 1;
  text-align: center;
}

.feedback-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

textarea {
  width: 100%;
  height: 100px;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  resize: vertical;
}

button {
  padding: 10px 20px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}
</style>
